package cn.kkcxy.blog.system.service.impl;

import cn.hutool.core.util.DesensitizedUtil;
import cn.kkcxy.blog.common.utils.RedisUtil;
import cn.kkcxy.blog.core.dto.PageParams;
import cn.kkcxy.blog.core.dto.PageResult;
import cn.kkcxy.blog.core.dto.admin.ManageGetUserListDTO;
import cn.kkcxy.blog.core.enums.LoginTypeEnum;
import cn.kkcxy.blog.core.exception.backend.SystemBusinessException;
import cn.kkcxy.blog.core.vo.admin.system.*;
import cn.kkcxy.blog.system.service.SystemService;
import cn.kkcxy.blog.system.service.sys.SysRoleService;
import cn.kkcxy.blog.system.service.sys.SysUserLoginLogService;
import cn.kkcxy.blog.system.service.sys.SysUserService;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @BelongsProject: kkcxy-blog
 * @BelongsPackage: cn.kkcxy.blog.system.service.impl
 * @Author: kkcxy
 * @CreateTime: 2024-11-18 00:20
 * @Description: 后台系统管理业务实现类
 * @Version: 1.0
 */
@Slf4j
@Service
public class SystemServiceImpl implements SystemService {

    @Resource
    private SysRoleService sysRoleService;

    @Resource
    private SysUserService sysUserService;

    @Resource
    private SysUserLoginLogService sysUserLoginLogService;

    @Resource
    private RedisUtil redisUtil;

    /**
     * 获取系统用户列表
     */
    @Override
    public ManageGetUserListVO getAllOfUser(ManageGetUserListDTO manageGetUserListDTO) {
        // 构建结果集
        ManageGetUserListVO manageGetUserListVO = new ManageGetUserListVO();
        // 查询搜索条件的角色列表
        List<SimpleRoleInfoVO> roles = sysRoleService.getUserSearchConditionsRoleList();
        manageGetUserListVO.setRoles(roles);
        // 查询用户列表
        PageResult<SimpleUserInfoVO> pageResult = new PageResult<>();
        // 取出分页变量
        PageParams pageParams = manageGetUserListDTO.getPageParams();
        Long pageNo = pageParams.getPageNo();
        Long pageSize = pageParams.getPageSize();
        // 构建分页参数
        PageHelper.startPage(ObjectUtils.isEmpty(pageNo) ? 1 : pageNo.intValue(), ObjectUtils.isEmpty(pageSize) ? 10 : pageSize.intValue());
        // 查询分页结果
        List<SimpleUserInfoVO> simpleUserInfoVOS = sysUserService.getSimpleUserInfoList(manageGetUserListDTO);
        // 封装分页结果
        PageInfo<SimpleUserInfoVO> pageInfo = new PageInfo<>(simpleUserInfoVOS);
        pageResult.setCount(pageInfo.getTotal());
        pageResult.setItems(simpleUserInfoVOS);
        manageGetUserListVO.setPageResult(pageResult);
        // 返回
        return manageGetUserListVO;
    }

    /**
     * 获取用户详情
     */
    @Override
    public UserDetailsVO getUserDetails(String userId) {
        // 简单判空
        if (StringUtils.isEmpty(userId)) {
            throw new SystemBusinessException("获取用户信息数据异常");
        }
        // 从Redis中看看有没有值
        String redisKey = redisUtil.buildKey("Backend:UserDetails:" + userId);
        UserDetailsVO vo = JSON.parseObject(redisUtil.get(redisKey), UserDetailsVO.class);
        // Redis中没有用户数据
        if (ObjectUtils.isEmpty(vo)) {
            // 获取用户账号信息
            vo = sysUserService.getUserDetails(userId);
            vo.setOpenId(StringUtils.isNotEmpty(vo.getOpenId()) ? DesensitizedUtil.idCardNum(vo.getOpenId(), 3, 3) : "用户暂未绑定微信");
            redisUtil.set(redisKey, JSON.toJSONString(vo));
        }
        // 查询用户近期登录日志
        List<UserLoginLogInfoVO> logVO = sysUserLoginLogService.getUserRecentLoginLog(userId);
        if (logVO.isEmpty()) {
            return vo;
        }
        for (UserLoginLogInfoVO log : logVO) {
            String typeName = LoginTypeEnum.getType(Integer.parseInt(log.getLoginType())).getName();
            log.setLoginType(typeName);
        }
        vo.setUserLoginLogs(logVO);
        return vo;
    }
}
